package com.goodving.guli.activity;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventUserSelected;
import com.goodving.guli.bt.eventbus.EventWeightChanged;
import com.goodving.guli.bt.eventbus.EventWeightSyncedDevice;
import com.goodving.guli.bt.model.WeightInfo;
import com.goodving.guli.bt.model.WeightInfoManager;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.ScaleWeightInfo;
import com.goodving.guli.bt.ui.WeightUserAvatar;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.bt.weight.LuaUtil;
import com.goodving.guli.bt.weight.UserInfo;
import com.goodving.guli.bt.weight.UserInfoManager;
import com.goodving.guli.view.C3247b;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.com.smartdevices.bracelet.chart.util.C0801q;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/9.
 */
public class UserListActivity extends DialogFragment { // C1982Q
    private static final String f11139e = "UserListActivity";
    private static final int f11140o = 250;
    private ListView f11141a;
    private TextView f11142b;
    private ScaleWeightInfo f11143c;
    private List<UserInfo> f11144d;
    private ac f11145f;
    private Context f11146g;
    private LinearLayout f11147h;
    private LinearLayout f11148i;
    private RelativeLayout f11149j;
    private boolean f11150k;
    private int f11151l;
    private Animator f11152m;
    private Animator f11153n;
    private aa f11154p;


    public UserListActivity() {
        this.f11144d = new ArrayList();
        this.f11150k = false;
        this.f11151l = 350;
    }

    class ac extends BaseAdapter {
        final /* synthetic */ UserListActivity f11328a;

        ac(UserListActivity UserListActivity) {
            this.f11328a = UserListActivity;
        }

        public int getCount() {
            return this.f11328a.f11144d.size();
        }

        public Object getItem(int i) {
            return this.f11328a.f11144d.get(i);
        }

        public long getItemId(int i) {
            return (long) i;
        }

        class ad {
            TextView f11329a;
            WeightUserAvatar f11330b;
            final /* synthetic */ ac f11331c;

            ad(ac acVar) {
                this.f11331c = acVar;
            }
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            ad adVar;
            GuliLog.tracePrintInfo("UserListActivity", "adapter getView()");
            if (view == null) {
                view = LayoutInflater.from(this.f11328a.f11146g).inflate(R.layout.userlist_item, null);
                ad adVar2 = new ad(this);
                adVar2.f11329a = (TextView) view.findViewById(R.id.user_list_item_name);
                adVar2.f11330b = (WeightUserAvatar) view.findViewById(R.id.user_icon);
                view.setTag(adVar2);
                adVar = adVar2;
            } else {
                adVar = (ad) view.getTag();
            }
            UserInfo userInfo = (UserInfo) this.f11328a.f11144d.get(i);
            if (!(userInfo.name == null || userInfo.name.isEmpty())) {
                adVar.f11329a.setText(userInfo.name);
            }
            adVar.f11330b.m15375a(userInfo.name);
            adVar.f11330b.m15374a(Utils.m8048c(((UserInfo) this.f11328a.f11144d.get(i)).uid));
            Utils.m8008a(userInfo, adVar.f11330b);
            return view;
        }
    }

    public void m15616a() {
        m15617a(250);
    }

    class C1988V implements AnimatorUpdateListener {
        final /* synthetic */ int f11175a;
        final /* synthetic */ UserListActivity f11176b;

        C1988V(UserListActivity UserListActivity, int i) {
            this.f11176b = UserListActivity;
            this.f11175a = i;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f11176b.f11148i.setY((float) (this.f11175a - ((Integer) valueAnimator.getAnimatedValue()).intValue()));
        }
    }

    class C1989W implements AnimatorUpdateListener {
        final /* synthetic */ UserListActivity f11177a;

        C1989W(UserListActivity UserListActivity) {
            this.f11177a = UserListActivity;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f11177a.f11147h.setAlpha(((Float) valueAnimator.getAnimatedValue()).floatValue());
        }
    }


    public void m15617a(long j) {
        if (this.f11152m == null || !this.f11152m.isRunning()) {
            int height = this.f11148i.getHeight();
            int height2 = this.f11149j.getHeight();
            ValueAnimator ofInt = ValueAnimator.ofInt(new int[]{0, height});
            ofInt.addUpdateListener(new C1988V(this, height2));
            ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
            ofFloat.addUpdateListener(new C1989W(this));
            C0801q.m8765a(ofInt);
            C0801q.m8769b(ofFloat);
            Animator a = C0801q.m8764a();
            a.setInterpolator(new DecelerateInterpolator());
            a.setDuration(j);
            a.start();
            this.f11152m = ofInt;
        }
    }

    class C1985T implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ int f11172a;
        final /* synthetic */ UserListActivity f11173b;

        C1985T(UserListActivity UserListActivity, int i) {
            this.f11173b = UserListActivity;
            this.f11172a = i;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int intValue = ((Integer) valueAnimator.getAnimatedValue()).intValue();
            GuliLog.tracePrintInfo("UserListActivity", "anim height " + intValue);
            this.f11173b.f11148i.setY((float) (this.f11172a - intValue));
        }
    }

    class C1986U implements AnimatorUpdateListener {
        final /* synthetic */ UserListActivity f11174a;

        C1986U(UserListActivity c1982q) {
            this.f11174a = c1982q;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            this.f11174a.f11147h.setAlpha(((Float) valueAnimator.getAnimatedValue()).floatValue());
        }
    }

    public void m15618a(long j, Animator.AnimatorListener animatorListener) {
        if (this.f11153n == null || !this.f11153n.isRunning()) {
            int height = this.f11148i.getHeight();
            int height2 = this.f11149j.getHeight();
            ValueAnimator ofInt = ValueAnimator.ofInt(new int[]{height, 0});
            ofInt.addUpdateListener(new C1985T(this, height2));
            ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{1.0f, 0.0f});
            ofFloat.addUpdateListener(new C1986U(this));
            C0801q.m8765a(ofInt);
            C0801q.m8769b(ofFloat);
            Animator a = C0801q.m8764a();
            a.setInterpolator(new AccelerateInterpolator());
            a.setDuration(j);
            a.addListener(animatorListener);
            a.start();
            this.f11153n = ofInt;
        }
    }

    public void m15619a(ListView listView) {
        this.f11151l = Utils.m7963a(this.f11146g, 350.0f);
        ListAdapter adapter = listView.getAdapter();
        if (adapter != null) {
            int i = 0;
            for (int i2 = 0; i2 < adapter.getCount(); i2++) {
                View view = adapter.getView(i2, null, listView);
                view.measure(0, 0);
                i += view.getMeasuredHeight();
            }
            ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
            layoutParams.height = (listView.getDividerHeight() * (adapter.getCount() - 1)) + i;
            layoutParams.height = Math.min(layoutParams.height, this.f11151l);
            listView.setLayoutParams(layoutParams);
        }
    }

    public void m15620a(aa aaVar) {
        this.f11154p = aaVar;
    }

    class C1991Y implements Animator.AnimatorListener {
        final /* synthetic */ UserListActivity f11275a;

        C1991Y(UserListActivity UserListActivity) {
            this.f11275a = UserListActivity;
        }

        public void onAnimationCancel(Animator animator) {
        }

        public void onAnimationEnd(Animator animator) {
            GuliLog.tracePrintInfo("UserListActivity", "anim end and dismiss");
            if (this.f11275a.f11154p != null) {
                this.f11275a.f11154p.m15724a();
            }
            this.f11275a.dismiss();
        }

        public void onAnimationRepeat(Animator animator) {
        }

        public void onAnimationStart(Animator animator) {
        }
    }

    public void m15621b() {
        m15618a(250, new C1991Y(this));
    }

    public void dismissAllowingStateLoss() {
        if (getActivity() != null) {
            super.dismissAllowingStateLoss();
            m15621b();
        }
    }


    class C1990X implements Runnable {
        final /* synthetic */ UserListActivity f11274a;

        C1990X(UserListActivity UserListActivity) {
            this.f11274a = UserListActivity;
        }

        public void run() {
            this.f11274a.m15616a();
        }
    }


    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        this.f11148i.post(new C1990X(this));
    }

    public void onCancel(DialogInterface dialogInterface) {
        GuliLog.tracePrintInfo(f11139e, "onCancel");
        m15621b();
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.f11146g = getActivity().getApplicationContext();
    }

    class C1983R implements View.OnClickListener {
        final /* synthetic */ UserListActivity f11155a;

        C1983R(UserListActivity UserListActivity) {
            this.f11155a = UserListActivity;
        }

        public void onClick(View view) {
            GuliLog.tracePrintInfo("UserListActivity", "onEmptyClick");
            this.f11155a.m15621b();
        }
    }

    class C1992Z extends LinearLayout {
        final /* synthetic */ UserListActivity f11276a;
        private TextView f11277b;
        private WeightUserAvatar f11278c;

        public C1992Z(UserListActivity UserListActivity, Context context) {
            this(UserListActivity, context, null);
        }

        public C1992Z(UserListActivity UserListActivity, Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            this.f11276a = UserListActivity;

            LayoutInflater.from(UserListActivity.f11146g).inflate(R.layout.userlist_item, this, true);
            this.f11277b = (TextView) findViewById(R.id.user_list_item_name);
            this.f11277b.setText(UserListActivity.getString(R.string.new_user));
            this.f11278c = (WeightUserAvatar) findViewById(R.id.user_icon);
            this.f11278c.setBackground(getResources().getDrawable(R.drawable.btn_statistic_mode_zoom_out));
        }
    }

    class C1984S implements AdapterView.OnItemClickListener {
        final /* synthetic */ UserListActivity f11171a;

        C1984S(UserListActivity UserListActivity) {
            this.f11171a = UserListActivity;
        }

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            GuliLog.tracePrintInfo("UserListActivity", "onItemClick " + i + " count " + adapterView.getAdapter().getCount());
            if (this.f11171a.f11143c == null) {
                EventBus.getDefault().post(new EventUserSelected(((UserInfo) adapterView.getAdapter().getItem(i)).uid));
            } else if (i < adapterView.getAdapter().getCount() - 1) {
                UserInfo a = UserInfoManager.getInstance().getInfoByUid(((UserInfo) adapterView.getAdapter().getItem(i)).uid);
                WeightInfoManager a2 = WeightInfoManager.getInstance();
                WeightInfo b = a2.m15760b(this.f11171a.f11143c.m25494e());
                if (b != null) {
                    a2.m15750a(b, a.uid);
                    if (a.uid == -1) {
//                        QQHealth.getInstance(this.f11171a.f11146g).addNeedSyncWeight(Long.valueOf(b.timestamp));
                    }
                    EventBus.getDefault().post(new EventWeightSyncedDevice(a.uid));
                } else {
                    GuliLog.tracePrintInfo("UserListActivity", "can not go here,something wrong!!!");
                }
                LuaUtil.getInstance(this.f11171a.f11146g).m15961a(this.f11171a.f11143c, a, WeightUtil.m15696a(this.f11171a.f11143c.m25494e(), 0));
                EventBus.getDefault().post(new EventWeightChanged(true, a.uid));
                Intent intent = new Intent(this.f11171a.f11146g, WeightReportActivity.class);
                intent.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11171a.f11143c.m25503n());
                intent.putExtra(UserInfo.USERINFO_KEY, a.toJsonString());
                GuliLog.tracePrintInfo("UserListActivity", "here userInfo toJsonString " + a.toJsonString());
                this.f11171a.startActivity(intent);
            } else if (!Utils.m8084k(this.f11171a.f11146g)) {
                C3247b.m22930a(this.f11171a.f11146g, this.f11171a.getString(R.string.please_connect_internet), 0).show();
                return;
            } else if (UserInfoManager.getInstance().m15595c() >= 16) {
                C3247b.m22930a(this.f11171a.f11146g, this.f11171a.getString(R.string.over_the_max_user_count), 0).show();
                return;
            } else {
                Intent intent2 = new Intent(this.f11171a.f11146g, MemberInfoSetNameActivity.class);
                intent2.putExtra("FROM_BABY_WEIGHT", this.f11171a.f11150k);
                intent2.putExtra(MemberInfoBaseActivity.weightChooseUser, true);
                intent2.putExtra(ScaleWeightInfo.WEIGHTADVDATA_KEY, this.f11171a.f11143c.m25503n());
                this.f11171a.startActivity(intent2);
                C0668a.m8128a(this.f11171a.f11146g, C0668a.fl, C0668a.fn);
                this.f11171a.m15621b();
                return;
            }
            this.f11171a.m15621b();
        }
    }

    class ab implements Comparator {
        float f11326a;
        final /* synthetic */ UserListActivity f11327b;

        public ab(UserListActivity c1982q, float f) {
            this.f11327b = c1982q;
            this.f11326a = f;
        }

        public int compare(Object obj, Object obj2) {
            float f = 0.0f;
            UserInfo userInfo = (UserInfo) obj2;
            WeightInfo e = WeightInfoManager.getInstance().m15777e(((UserInfo) obj).uid);
            WeightInfo e2 = WeightInfoManager.getInstance().m15777e(userInfo.uid);
            float abs = e != null ? Math.abs(e.weight - this.f11326a) : 0.0f;
            if (e2 != null) {
                f = Math.abs(Math.abs(e2.weight - this.f11326a));
            }
            return new Float(abs).compareTo(new Float(f));
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_userslist, viewGroup, false);
        this.f11141a = (ListView) inflate.findViewById(R.id.users_list);
        this.f11142b = (TextView) inflate.findViewById(R.id.list_title);
        this.f11147h = (LinearLayout) inflate.findViewById(R.id.dlg_empty_area_btn);
        this.f11148i = (LinearLayout) inflate.findViewById(R.id.content);
        this.f11149j = (RelativeLayout) inflate.findViewById(R.id.main_content);
        this.f11147h.setOnClickListener(new C1983R(this));
        this.f11144d.clear();
        Bundle arguments = getArguments();
        if (arguments != null) {
            String string = arguments.getString(ScaleWeightInfo.WEIGHTADVDATA_KEY);
            if (string != null) {
                this.f11143c = ScaleWeightInfo.m25479b(string);
                GuliLog.tracePrintInfo(f11139e, "receive weightadvdata " + this.f11143c.toString());
            }
            this.f11150k = arguments.getBoolean("FROM_BABY_WEIGHT");
        }
        GuliLog.tracePrintInfo(f11139e, "mUserInfos size is " + this.f11144d.size() + " " + toString());
        this.f11144d = UserInfoManager.getInstance().m15598d();
        this.f11142b.setText(getString(R.string.weight_match_no_suitable_user));
        if (this.f11143c == null) {
            this.f11142b.setText(getString(R.string.please_choose_user));
            this.f11144d.clear();
            this.f11144d = UserInfoManager.getInstance().m15598d();
        }
        if (this.f11143c != null) {
            Collections.sort(this.f11144d, new ab(this, WeightUtil.m15689a(this.f11143c.m25499j(), this.f11143c.m25497h())));
        }
        for (UserInfo userInfo : this.f11144d) {
            GuliLog.tracePrintInfo(f11139e, "after info : " + userInfo.toString());
        }
        this.f11145f = new ac(this);
        this.f11141a.setAdapter(this.f11145f);
        if (this.f11143c != null) {
            this.f11141a.addFooterView(new C1992Z(this, this.f11146g));
        }
        this.f11141a.setOnItemClickListener(new C1984S(this));
        m15619a(this.f11141a);
        return inflate;
    }

    public void onPause() {
        super.onPause();
        C0668a.m8141b(C0668a.aR);
    }

    public void onResume() {
        super.onResume();
        C0668a.m8133a(C0668a.aR);
    }
}
